/*********************************************************
  该文件实现mainwindow的gui和串口的相关函数
   author：wulin
  *********************************************************/
#include "AdapterSerialPort.h"
#include "settingsdialog.h"
#include "macro.h"
#include <QMessageBox>

/****槽函数，将dialog中的参数传给主窗口****/
 void AdapterSerialPort::setPortSettings(const PortSettings& portSettings)
{
    *myPortSettings=portSettings;
}
 void AdapterSerialPort::setPortName(const QString& name)
{
    *myPortName=name;
}
 /******打开串口***********************/
void AdapterSerialPort::on_openComBtn_clicked()
{
    myCom = new QextSerialPort(*myPortName,*myPortSettings,QextSerialPort::EventDriven);
    //定义串口对象，并传递参数，在构造函数里对其进行初始化
    myCom ->open(QIODevice::ReadWrite); //打开串口

    if(myCom->isOpen())  //如果打开成功
    {
        textEdit->append("This com is opened successfully");
        connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
        //信号和槽函数关联，当串口缓冲区有数据时，进行读串口操作.
        //只能在EventDriven模式下工作，Linux下不支持。?

        openComBtn->setEnabled(false); //打开串口后“打开串口”按钮不可用
        closeComBtn->setEnabled(true); //打开串口后“关闭串口”按钮可用
        sendMsgBtn->setEnabled(true); //打开串口后“发送数据”按钮可用

    }
    else  //如果打开失败,显示错误信息
    {   textEdit->append("This com hasn't been opend");

         textEdit->append(myCom->errorString());
         //textEdit->insertPlainText("\n");
    }

}

/***** "关闭串口" 按钮的事件函数**********************/
void AdapterSerialPort::on_closeComBtn_clicked()
{
    temp->clear();
    myCom->close();     //关闭串口，该函数在win_qextserialport.cpp文件中定义
    openComBtn->setEnabled(true); //关闭串口后“打开串口”按钮可用
    closeComBtn->setEnabled(false); //关闭串口后“关闭串口”按钮不可用
    sendMsgBtn->setEnabled(false); //关闭串口后“发送数据”按钮不可用
    textEdit->append("This com has been closed succesfully");
   // textEdit->insertPlainText("\n");
   // disconnect(timer, SIGNAL(timeout()), this, SLOT(readMyCom()));
    delete myCom; myCom=NULL;
}

/******************* "发送"按钮  *******************/
void AdapterSerialPort::on_sendMsgBtn_clicked()
{
    myCom->write(QByteArray::fromHex(sendMsgEditLine->text().simplified().toLatin1()));
}

/******************* "设置"按钮  *******************/
void AdapterSerialPort::on_settingsBtn_clicked()
{
    Dialog=new settingsDialog(this);  //以当前主窗口为父对象
    if(Dialog)  //如果内存申请成功
    {
     connect(Dialog,SIGNAL(signalPortSettings(const PortSettings &)),
      this,SLOT(setPortSettings(const PortSettings&)) );//将对话框中的设置传给主窗口
     connect(Dialog,SIGNAL(signalMyComName(const QString&)),
      this,SLOT(setPortName(const QString&)) );//将对话框中的设置传给主窗口
    Dialog->setAttribute(Qt::WA_DeleteOnClose); //一旦关闭则删除
    Dialog->activateWindow();Dialog->raise();
    Dialog->exec();  //将对话框设置为模态，防止打开对话框的同时又按下“设置”键
    }
}

void AdapterSerialPort::on_clearBtn_clicked()
{
    textEdit->clear();
}

void AdapterSerialPort::on_exitBtn_clicked()
{
    QMessageBox msgBox;
    msgBox.setIcon(QMessageBox::Warning);
     msgBox.setText("Do you want to close?");
     msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel );
     msgBox.setDefaultButton(QMessageBox::Cancel);
     int ret = msgBox.exec();
     switch (ret) {
       case QMessageBox::Ok:
         exit(0);
          break;
       case QMessageBox::Cancel:
           // Cancel was clicked
           break;
       default:
           // should never be reached
           break;
       }
}

void AdapterSerialPort::on_checkBoxNor_clicked()
{

}

void AdapterSerialPort::on_checkBoxNor_toggled(bool checked)
{
    if(!checkBoxNor->isChecked()&&dbo==NULL)
    {

    }
    else if(checkBoxNor->isChecked()&&dbo!=NULL)
    {
        //delete dbo;
        //dbo=NULL;
    }
}



